package com.itheima.springboottlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.springboottlias.annotation.SysLog;
import com.itheima.springboottlias.mapper.EmpMapper;
import com.itheima.springboottlias.pojo.Emp;
import com.itheima.springboottlias.pojo.PageBean;
import com.itheima.springboottlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Taotao
 * @date 2023/7/15 21:45
 * @desciption:
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean page(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        Page<Emp> emps = empMapper.list(name, gender, begin, end);
        long total = emps.getTotal();
        List<Emp> empList = emps.getResult();
        PageBean pageBean = new PageBean(total, empList);
        return pageBean;


    }

    @Override
    @SysLog
    public void deleteById(List<Integer> ids) {
        empMapper.deleteById(ids);
    }

    @Override
    @SysLog
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        emp.setPassword("123456");
        empMapper.addEmp(emp);
    }

    @Override
    public Emp selectById(Integer id) {
        Emp emp = empMapper.selectById(id);
        emp.setPassword(null);
        return emp;
    }

    @Override
    @SysLog
    public void updateById(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
    }

    @Override
    public Emp login(String username, String password) {
       Emp emp = empMapper.selectNameAndPassword(username , password);
        return emp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class ,
            propagation = Propagation.REQUIRES_NEW)
    @SysLog
    public void deleteByDeptId(Integer id) {
//        删除员工
        empMapper.deleteByDeptId(id);
    }


}
